package com.easyexcle.demo.easyExcel.util;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

/**
 * @Author: lkz
 * @Title: DateRangeCalculator
 * @Description: 计算时间 天 月 周 季度
 * @Date: 2023/5/20 9:42
 */

public class DateRangeCalculator {


    public static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2023, 1, 1); // 开始日期
        LocalDate endDate = LocalDate.of(2024, 12, 31); // 结束日期

        System.out.println("日:");
        printDateRange(startDate, endDate, ChronoUnit.DAYS);

        System.out.println("\n周:");
        printDateRange(startDate, endDate, ChronoUnit.WEEKS);

        System.out.println("\n月:");
        printDateRange(startDate, endDate, ChronoUnit.MONTHS);

        System.out.println("\n年:");
        printDateRange(startDate, endDate, ChronoUnit.YEARS);
    }

    private static void printDateRange(LocalDate startDate, LocalDate endDate, ChronoUnit unit) {
        LocalDate current = startDate;
        while (!current.isAfter(endDate)) {
            LocalDate endOfRange = current.plus(1, unit).minus(1, ChronoUnit.DAYS);
            System.out.println("From " + current + " to " + endOfRange);
            current = current.plus(1, unit);
        }
    }


}
